package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.OrderItemsFight;
import com.addplus.townmall.server.api.model.town.expand.TownOrderFightExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 拼团商品订单子项详情表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-10-30
 */
public interface OrderItemsFightMapper extends BaseAddMapper<OrderItemsFight> {

    @Select("SELECT DISTINCT oif.order_id FROM order_items_fight oif\n" +
            "WHERE oif.is_delete = 0 AND oif.shop_product_id = #{shopProductId}")
    List<Integer> getOrderItemsFightByShopProductId(Integer shopProductId);


    @Select("SELECT\n" +
            "*"+
            "FROM\n" +
            " order_items_fight \n" +
            "WHERE\n" +
            " order_id = #{orderId} \n" +
            " AND is_delete = 0 \n" +
            "ORDER BY\n" +
            " id")
    List<OrderItemsFight> getOrderItemsFightDetail(@Param("orderId") Integer orderId);

    @Select("SELECT\n" +
            " f.id,\n" +
            " m.head_portrait,\n" +
            " m.nick_name as member_name ,\n" +
            " f.group_num,\n" +
            " f.join_group_num,\n" +
            " f.fight_last_time,\n" +
            " f.gmt_create\n" +
            "FROM\n" +
            " town_order_fight f\n" +
            " LEFT JOIN order_items_fight i ON i.order_id = f.id \n" +
            " AND i.is_delete = 0\n" +
            " LEFT JOIN shop_product_fight s ON i.shop_product_id = s.id \n" +
            " AND s.online_status = 1 \n" +
            " AND s.sale_status = 0 \n" +
            " LEFT JOIN member m ON f.member_id = m.id \n" +
            " AND m.is_deleted = 0\n" +
            "WHERE\n" +
            " i.shop_product_id = #{shopProductFightId} \n" +
            " AND f.order_status = 2 \n" +
            " AND f.pay_status = 4 \n" +
            " AND f.is_group = 1 \n" +
            " AND f.parent_id = 0 \n" +
            " AND f.is_success = 0 \n" +
            " AND f.is_delete = 0\n" +
            " ORDER BY f.gmt_create desc")
    List<TownOrderFightExpand> getFightGroupUnSuccessOrder(Page page, @Param("shopProductFightId") Integer shopProductFightId);


    @Select("select sum(cost_price*purchase_quantity) as sumCostPrice from town_order_fight WHERE order_id=#{orderId} ")
    BigDecimal getSumCostPrice(@Param("orderId") Integer orderId);

    @Select("<script>\n" +
            "SELECT *" +
            " from order_items  a " +
            "where " +
            "a.id in " +
            "<foreach collection='Items' open='('  item='item'  separator=',' close=')'> #{item}</foreach> " +
            "and order_id=#{orderId} \n"+
            "</script>")
    List<OrderItemsFight> getItemsByIds(@Param("Items") List<String> Items,@Param("orderId") Integer orderId);
}
